package com.atguigu.tingshu.album.task;

import com.atguigu.tingshu.album.service.AuditService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * ClassName : ReviceResultTask
 * Package : com.atguigu.tingshu.album.task
 * Description :
 * <p>
 * Author: LHZ
 * Create: 2025/7/18 23:59
 * Version: 1.0
 */
@Component
@Slf4j
public class ReviceResultTask {

    @Autowired
    private TrackInfoService trackInfoService;
    @Autowired
    private AuditService auditService;
    @Autowired
    private RedissonClient redissonClient;


    @Scheduled(cron = "0/2000 * * * * ?")
    public void reviceResultJob(){
        //获取分布式锁
        RLock lock = redissonClient.getLock("reviewtask:lock");
        boolean isFlag = lock.tryLock();

        if(isFlag){
            try {
                log.info("开始获取审核结果");
                //查询所有状态为人工复审的音频数据
                LambdaQueryWrapper<TrackInfo> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(TrackInfo::getStatus, SystemConstant.TRACK_STATUS_REVIEW)
                        .select(TrackInfo::getId,TrackInfo::getReviewTaskId)
                        .orderByAsc(TrackInfo::getId)
                        .last("limit 100");
                List<TrackInfo> trackInfoList = trackInfoService.list(queryWrapper);

                if(!CollectionUtils.isEmpty(trackInfoList)){
                    for (TrackInfo trackInfo : trackInfoList) {
                        String suggestion = auditService.getReviewTaskResult(trackInfo.getReviewTaskId());
                        //如果审核建议不为空,说明已经审核完毕
                        if(StringUtils.isNotBlank(suggestion)){
                            //根据审核结果设置对应的音频状态
                            if("pass".equals(suggestion)){
                                trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
                            }else if("review".equals(suggestion)){
                                trackInfo.setStatus(SystemConstant.TRACK_STATUS_REVIEW);
                            }else if("block".equals(suggestion)){
                                trackInfo.setStatus(SystemConstant.TRACK_STATUS_NO_PASS);
                            }
                            trackInfoService.update(trackInfo,null);
                        }
                    }
                }
                System.out.println("此轮审核结束");
            } finally {
                lock.unlock();
            }
        }

    }
}